// 第一题
// 给定一个数组将该数组的o项放到最后
// 例如：[1, 3, 4, 0, 22, 45, 0, 9] => [1, 3, 4, 22, 45, 9, 0, 0]
//第一种方法：
var arr1 = [1, 3, 4, 0, 22, 45, 0, 9];
function lastZero1(arr) {
    var notzeroNum = arr.filter(num => num).length;
    return arr.filter(num => num).concat(Array(arr.length - notzeroNum).fill(0));
 }
 console.log(lastZero1(arr1));

 // 第二种
 var arr2 = [0, 0, 1, 3, 4, 0, 22, 45, 0, 9];
 function lastZero2(arr) {
     var  num = 0;
     for(var i = 0; i < arr.length - num; i++) {
         if (arr[i] === 0) {
             arr.splice(i, 1);
             arr.push(0);
             num++;
             i--;
         };
     }
     return arr;
 }
 console.log(lastZero2(arr2));


 // 第三种--排序算法的味道
//  [1, 0, 3, 0,0, 5] => i = 0;
//  [1, 0, 3, 0, 5] => i = 1;
//  [1, 3, 0, 0, 5] => i = 2;
//  [1, 3, 0, 0, 5] => i = 2;
//  [1, 3, 5, 0, 0] => i = 4;
var arr3 = [1, 3, 0, 0, 8, 0];
function lastZero3(arr) {
    var poit = -1;
    for(var i = 0; i < arr.length; i++) {
        if (arr[i] === 0) {
            poit = i;
            arr[i + 1] === 0 && i++;
        } else {
            if (poit <= i && poit > -1) {
                var temp = arr[i];
                arr[poit] = temp;
                arr[i] = 0;
                poit = i;
            };
        }
    };
    return arr;
};
console.log(lastZero3(arr3));